<?php
/**
 * 数据导入
 * @author jianhui
 *
 */
class Import extends MY_Controller{
    var $navbar = array();
    function Import(){
        parent::MY_Controller();
        if (!$this->auth->isImport()){
            showError('您无此页面操作权限','/');
        }
        $this->load->model('sellpointModel');
        $this->load->model('importMoneyModel');
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Import', 'index'),'title'=>'数据导入');
        //标签菜单
        $this->navbar = array(
            array('title'=>'首页','url'=>printUrl('Import','index'),'focusTag'=>'index'),
            array('title'=>'标准动作','url'=>printUrl('Import','actionList'),'focusTag'=>'actionList'),
            array('title'=>'单店双月来店目标','url'=>printUrl('Import','importList',array('type'=>13)),'focusTag'=>'importList13'),
            array('title'=>'单店基金双月额度','url'=>printUrl('Import','importList',array('type'=>11)),'focusTag'=>'importList11'),
            array('title'=>'单店基金项目定额','url'=>printUrl('Import','singleProjectFixed'),'focusTag'=>'singleProjectFixed'),
            array('title'=>'单店基金天网额度','url'=>printUrl('Import','importList',array('type'=>12)),'focusTag'=>'importList12'),
            array('title'=>'单店基金双月来店实际','url'=>printUrl('Import','importList',array('type'=>14)),'focusTag'=>'importList14'),
            array('title'=>'单店基金标准动作完成率','url'=>printUrl('Import','importList',array('type'=>15)),'focusTag'=>'importList15'),
            array('title'=>'城市提升基金双月额度','url'=>printUrl('Import','importList',array('type'=>21)),'focusTag'=>'importList21'),
            array('title'=>'城市提升基金结算金额','url'=>printUrl('Import','importList',array('type'=>22)),'focusTag'=>'importList22'),
            array('title'=>'车展基金广宣活动补贴','url'=>printUrl('Import','importList',array('type'=>31)),'focusTag'=>'importList31'),
            array('title'=>'车展基金启辰店实际出资总额','url'=>printUrl('Import','importList',array('type'=>32)),'focusTag'=>'importList32'),
            array('title'=>'车展基金配套额度','url'=>printUrl('Import','importList',array('type'=>33)),'focusTag'=>'importList33'),
            array('title'=>'车展基金本站结算金额','url'=>printUrl('Import','importList',array('type'=>34)),'focusTag'=>'importList34'),
            array('title'=>'车展基金KPI系数','url'=>printUrl('Import','importList',array('type'=>35)),'focusTag'=>'importList35'),
            array('title'=>'固定传播基金年度额度','url'=>printUrl('Import','importList',array('type'=>51)),'focusTag'=>'importList51'),
            array('title'=>'固定传播基金专营店类','url'=>printUrl('Import','spreadStore'),'focusTag'=>'spreadStore'),
            array('title'=>'固定传播基金双月扣罚金额','url'=>printUrl('Import','importList',array('type'=>52)),'focusTag'=>'importList52'),
            array('title'=>'专项单店基金双月额度','url'=>printUrl('Import','importList',array('type'=>61)),'focusTag'=>'importList61'),
            array('title'=>'专项城市基金双月额度','url'=>printUrl('Import','importList',array('type'=>71)),'focusTag'=>'importList71'),
        );
        $this->viewData['focusTag'] = $this->input->get('m');
    }
    /**
     * 列表
     */
    function index(){
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $this->load->view('Import/index', $this->viewData);
    }
    /**
     * 标准动作列表
     */
    function actionList(){
        //标准动作数据
        $this->load->model('actionModel');
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where['year'] = $year;
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit && $where['timeUnit'] = $timeUnit;
        //基金类型
        $type = (int)$this->input->get('type');
        $type && $where['type'] = $type;
        $this->viewData['timeUnit'] = $timeUnit;
        $this->viewData['type'] = $type;
        $this->viewData['actionList'] = $this->actionModel->getNewList($where,'','','','orderNum asc,id asc');
        $this->load->view('Import/actionList', $this->viewData);
    }
    /**
     * 添加标准动作
     */
    function actionAdd(){
        if ($_POST){
            $this->load->model('actionModel');
            $where = array(
                'year'      => $this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'name'      => $this->input->post('name'),
                'type'      => (int)$this->input->post('type'),
            );
            $data = array(
                'orderNum'  => (int)$this->input->post('orderNum'),
                'status'    => 1,
                'createTime'=> time()
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $where['type'] || showError('请选择基金类别');
            $where['name'] || showError('标准动作名称不能为空');
            if ($this->actionModel->getData($where)){
                showError('您已经添加过同名标准动作');
            }
            if ($this->actionModel->add(array_merge($where, $data))){
                //更新缓存
                $this->actionModel->createActionCache();
                showSuccess('添加标准动作成功',printUrl('Import', 'index', array('tab'=>'action')) );
            }else{
                showError('添加失败');
            }
        }else{
            $this->viewData['year'] = $this->config->item('nowYear');
            $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
            $this->load->view('Import/actionAdd', $this->viewData);
        }
    }
    /**
     * 修改标准动作
     */
    function actionEdit(){
        $this->load->model('actionModel');
        if($_POST){
            $id = (int)$this->input->post('id');
            $where = array(
                'year'      => $this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'name'      => $this->input->post('name'),
                'type'      => (int)$this->input->post('type'),
            );
            $data = array(
                'orderNum'  => (int)$this->input->post('orderNum'),
                'status'    => 1,
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $where['type'] || showError('请选择基金类别');
            if ($this->actionModel->getData(array_merge($where, array('id !=' => $id) ))){
                showError('您已经添加过同名标准动作');
            }
            if ($this->actionModel->edit($id, array_merge($where, $data))){
                //更新缓存
                $this->actionModel->createActionCache();
                showSuccess('修改标准动作成功',printUrl('Import', 'index', array('tab'=>'action')) );
            }else{
                showError('修改失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要修改标准动作');
            $this->viewData['data'] = $this->actionModel->getData($id);
            //配置
            $this->viewData['yearList']     = $this->timeUnitModel->getCacheData('yearList');
            $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
            $year = $this->config->item('nowYear');
            $this->viewData['timeUnitList'] = $timeUnitList[$year];
            $this->load->view('Import/actionEdit', $this->viewData);
        }
    }
    /**
     * 更改标准动作状态
     */
    function actionStatus(){
        $where['id'] = (int)$this->input->post('id');
        $where['id'] || showError('请选择要操作的标准动作');
        $this->load->model('actionModel');
        $findData = $this->actionModel->getData($where);
        $data = array(
            'status' => $findData['status'] == 1 ? 0 : 1,
        );
        if ($this->actionModel->edit($where, $data)){
            //更新缓存
            $this->actionModel->createActionCache();
            showSuccess('更改状态成功');
        }else{
            showError('更改状态失败');
        }
    }
    /**
     * 导入金额列表
     */
    function importList(){
        $type = (int)$this->input->get('type');
        $type || $type = 1;
        $this->viewData['type'] = $type;
        $where = array('type'=>$type);
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $where['year'] = $year;
        $this->viewData['year'] = $year;
        //时间段
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit && $where['timeUnit'] = $timeUnit;;
        $this->viewData['timeUnit'] = $timeUnit;
        //关键字
        $keyword = $this->input->get('keyword');
        $keyword && $where['storeName'] = array('like',$keyword);
        $this->viewData['keyword'] = $keyword;
        //载入模型
        $count = $this->importMoneyModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('Import', 'importList', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        $this->viewData['list'] = $this->importMoneyModel->getNewList($where, $limit, $page, '', 'id desc');
        /* if ($type == 13){
            $this->viewData['totalMoney'] = $this->importMoneyModel->getSum($where,'money');
        } */
        //设置选中标签
        $this->viewData['focusTag'] = $this->input->get('m').$this->input->get('type');
        $this->viewData['typeName'] = $this->importMoneyModel->getTypeName($type);
        $this->load->view('Import/importList', $this->viewData);
    }
    /**
     * 单店基金项目定额导入
     * @return boolean
     */
    function importAdd(){
        if ($_POST){
            $year = (int)$this->input->post('year');
            $year || showError('请选择年份');
            $type = (int)$this->input->post('type');
            $type || showError('请选择类型');
            $timeUnit = 0;
            if (!in_array($type, array(51))){//固定传播基金
                $timeUnit = (int)$this->input->post('timeUnit');
                $timeUnit || showError('请选择双月');
            }
            //上传文件
            $fileData = $this->upload();
            $fullPath = $fileData['full_path'];
            $handle = fopen ($fullPath, "r");
            $i = 0; $ok = 0; $err = 0; $totalMoney = 0;
            $errorSellpoint = array();
            //载入单店主线流程模型
            $this->load->model('singleProcessModel');
            //载入城市基金主线流程模型
            $this->load->model('cityProcessModel');
            //载入车展即将主线流程模型
            $this->load->model('carProcessModel');
            //载入固定传播基金主线流程模型
            $this->load->model('spreadProcessModel');
            //载入专项单店基金主线流程模型
            $this->load->model('specialProcessModel');
            //载入专项城市基金主线流程模型
            $this->load->model('specityProcessModel');
            
            while ($item = fgetcsv($handle, 10000, ",")){
                if($i==0){$i++;continue;}//跳过第一行
                $coding = $item[5];
                $storeData = $this->sellpointModel->getNewData(array('Coding'=>$coding, 'SpState_sys'=>1,'isOpen'=>1));
                if (!$storeData){
                    $err ++;
                    $errorSellpoint[] = "【".gbk2utf8($item[4])."】的编码【".$coding."】错误";
                    continue;
                }
                $where = array(
                    'storeId' => (int)$storeData['userId'],
                    'timeUnit' => (int)$timeUnit,
                    'year' => (int)$year,
                    'type' => $type,
                );
                $data = array(
                    'area' => $storeData['area'],
                    'region' => $storeData['region'],
                    'spsarea' => $storeData['spsarea'],
                    'city' => $storeData['city'],
                    'coding' => $storeData['coding'],
                    'storeName' => $storeData['storeName'],
                    'money' => (float)$item[6],
                    'createTime' => time()
                );
                if ($this->importMoneyModel->upset($where, $data)){
                    $ok ++;
                    $totalMoney += (float)$item[6];
                    //后续处理
                    switch ($type){
                        case 11://单店基金双月额度
                            //生成主线流程数据并更新固定额度
                            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->singleProcessModel->updateMoney($procId);
                            break;
                        case 12://单店基金天网行动双月额度
                            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            //更新天网汇总金额
                            $this->load->model('singleNetModel');
                            $netData = $this->singleNetModel->getData(array('storeId'=>$where['storeId'],'procId'=>$procId));
                            if ($netData){
                                $this->singleNetModel->updateMoney($netData['id']);                                
                            }
                            break;
                        case 13://单店基金来店目标
                            //生成主线流程数据并更新固定额度
                            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->singleProcessModel->updateMoney($procId);
                            break;
                        case 14://单店基金来店实际
                            //生成主线流程数据并更新固定额度
                            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->singleProcessModel->updateMoney($procId);
                            break;
                        case 15://单店基金标准动作完成率导入模板
                            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->singleProcessModel->updateMoney($procId);
                            break;
                        case 21://城市提升基金双月额度
                            $procId = $this->cityProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->cityProcessModel->updateMoney($procId);
                            break;
                        case 22://城市提升基金结算金额
                            //生成主线流程数据并更新固定额度
                            $procId = $this->cityProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            //重新计算金额（坑爹的计算方式）
                            $this->cityProcessModel->updateMoney($procId);
                            break;
                        case 31: //车展基金广宣活动补贴
                            $procId = $this->carProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->load->model('carGxModel');
                            $list = $this->carGxModel->getList(array('storeId'=>$where['storeId'], 'procId'=>$procId));
                            foreach ($list as $v){
                                $this->carGxModel->updateMoney($v['id']);
                            }
                            break;
                        case 32: //车展基金启辰店实际出资总额
                            $procId = $this->carProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->load->model('carRentModel');
                            $list = $this->carRentModel->getList(array('storeId'=>$where['storeId'], 'procId'=>$procId));
                            foreach ($list as $v){
                                $this->carRentModel->updateMoney($v['id']);
                            }
                            break;
                        case 33: //车展基金配套额度
                            $procId = $this->carProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->load->model('carMatchingModel');
                            $list = $this->carMatchingModel->getList(array('storeId'=>$where['storeId'], 'procId'=>$procId));
                            foreach ($list as $v){
                                $this->carMatchingModel->updateMoney($v['id']);
                            }
                            break;
                        case 34: //车展基金本站结算金额
                            $procId = $this->carProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->carProcessModel->updateMoney($procId);
                            break;
                        case 35: //车展基金KPI系数
                            $procId = $this->carProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->carProcessModel->updateMoney($procId);
                            break;
                        case 51://固定传播基金年度额度
                            //生成主线流程数据并更新固定额度
                            $procData = $this->spreadProcessModel->getData(array('storeId'=>$where['storeId'], 'year'=>$where['year']));
                            //重新计算金额
                            $procData && $this->spreadProcessModel->updateMoney($procData['id']);
                            /*$procWhere = array(
                                'storeId' =>$where['storeId'],
                                'year' => $where['year'],
                            );
                            $initData = array(
                                'storeList' => serialize(array()),
                                'lostMoney' => serialize(array()),
                                'createTime' => time(),
                            );
                            $procId = $this->spreadProcessModel->upset($procWhere, $initData);
                            $procId && $this->spreadProcessModel->updateMoney($procId);*/
                            break;
                        case 52://固定传播基金双月扣罚金额
                            $procData = $this->spreadProcessModel->getData(array('storeId'=>$where['storeId'], 'year'=>$where['year']));
                            //重新计算金额
                            $procData && $this->spreadProcessModel->updateMoney($procData['id']);
                            //生成主线
                            /*$procWhere = array(
                                'storeId' =>$where['storeId'],
                                'year' => $where['year'],
                            );
                            $initData = array(
                                'storeList' => serialize(array()),
                                'lostMoney' => serialize(array()),
                                'createTime' => time(),
                            );
                            $procId = $this->spreadProcessModel->upset($procWhere, $initData);
                            $procId && $this->spreadProcessModel->updateMoney($procId);*/
                            break;
                        case 61://专项单店基金双月额度
                            $procId = $this->specialProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->specialProcessModel->updateMoney($procId);
                            break;
                        case 71://专项城市基金双月额度
                            $procId = $this->specityProcessModel->initData(array('storeId'=>$where['storeId'], 'year'=>$where['year'],'timeUnit'=>$where['timeUnit']));
                            $this->specityProcessModel->updateMoney($procId);
                            break;
                        default:
                    }
                }else{
                    $err ++;
                }
                usleep(500);
            }
            $msg = '导入完成：（本次共导入'.($ok+$err).'条记录，其中成功'.$ok.'条';
            if ($type == 13){
                $msg .= '，共计导入来店量：'.$totalMoney;
            }
            $msg .= ')';
            $errorSellpoint && $msg .= "<br>".implode('<br>',$errorSellpoint);
            showSuccess($msg);
        }else{
            //年
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            $type  = (int)$this->input->get('type');
            $type || showError('请选择导入类型');
            $this->viewData['type'] = $type;
            $this->viewData['typeName'] = $this->importMoneyModel->getTypeName($type);
            $templateFile = array(
                11 => 'singleFixed.zip',
                12 => 'singleNetFixed.zip',
                13 => 'singleStoreReachTarget.zip',
                14 => 'singleStoreReachActual.zip',
                15 => 'singleStandardRate.zip',
                21 => 'cityFixed.zip',
                22 => 'citySettle.zip',
                31 => 'carGxSubsidyMoney.zip',
                32 => 'carRentQiTotal.zip',
                33 => 'carMatchingSubsidyMoney.zip',
                34 => 'carActualSettleMoney.zip',
                35 => 'carKpi.zip',
                51 => 'spreadYear.zip',
                52 => 'spreadLose.zip',
                61 => 'specialFixedMoney.zip',
                71 => 'specityFixedMoney.zip',
            );
            $this->viewData['templateFile'] = isset($templateFile[$type]) ? 'public/template/'.$templateFile[$type] : '';
            $this->load->view('Import/importAdd', $this->viewData);
        }
    }
    /**
     * 单店基金-固定额度
     */
    function singleProjectFixed(){
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $where['year'] = $year;
        $this->viewData['year'] = $year;
        //关键字
        $keyword = $this->input->get('keyword');
        $keyword && $where['city'] = array('like',$keyword);
        $this->viewData['keyword'] = $keyword;
        //载入模型
        $this->load->model('singlePFixedModel');
        $count = $this->singlePFixedModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('Import', 'singleProjectFixed', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        $this->viewData['list'] = $this->singlePFixedModel->getList($where, $limit, $page, '', 'id desc');
    
        $this->load->view('Import/singleProjectFixed', $this->viewData);
    }
    /**
     * 单店基金-项目额度导入
     * @return boolean
     */
    function singleProjectFixedAdd(){
        if ($_POST){
            $year = (int)$this->input->post('year');
            $year || showError('请选择年份');
            //上传文件
            $fileData = $this->upload();
            $fullPath = $fileData['full_path'];
            $handle = fopen ($fullPath, "r");
            $i = 0; $ok = 0; $err = 0;
            $errorSellpoint = array();
            //载入单店主线流程模型
            $this->load->model('singlePFixedModel');
            while ($item = fgetcsv($handle, 10000, ",")){
                if($i==0){$i++;continue;}//跳过第一行
                $where = array(
                    'year' => (int)$year,
                    'city' => trim(gbk2utf8($item[1])),
                );
                $data = array(
                    'systemCity'    => trim(gbk2utf8($item[0])),
                    'cityLevel'     => (int)$item[2],
                    'diantouMoney'  => (float)$item[3],
                    'huwaiMoney'    => (float)$item[4],
                    'createTime'    => time()
                );
                if ($this->singlePFixedModel->upset($where, $data)){
                    $ok ++;
                }else{
                    $err ++;
                }
                usleep(500);
            }
            showSuccess('导入完成：（成功条数：'.$ok.',失败条数：'.$err.'）', '', 3);
        }else{
            //年
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            $this->load->view('Import/singleProjectFixedAdd', $this->viewData);
        }
    }
    /**
     * 固定传播基金专营店类列表
     */
    function spreadStore(){
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $where['year'] = $year;
        $this->viewData['year'] = $year;
        //关键字
        $keyword = $this->input->get('keyword');
        $keyword && $where['storeName'] = array('like',$keyword);
        $this->viewData['keyword'] = $keyword;
        //载入模型
        $this->load->model('spreadStoreModel');
        $count = $this->spreadStoreModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('Import', 'spreadStore', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        $this->viewData['list'] = $this->spreadStoreModel->getList($where, $limit, $page, '', 'id desc');
        
        $this->load->view('Import/spreadStore', $this->viewData);
    }
    /**
     * 固定传播基金专营店类新增
     * @return boolean
     */
    function spreadStoreAdd(){
        if ($_POST){
            $year = (int)$this->input->post('year');
            $year || showError('请选择年份');
            //上传文件
            $fileData = $this->upload();
            $fullPath = $fileData['full_path'];
            $handle = fopen ($fullPath, "r");
            $i = 0; $ok = 0; $err = 0;
            $errorSellpoint = array();
            //载入单店主线流程模型
            $this->load->model('spreadStoreModel');
            while ($item = fgetcsv($handle, 10000, ",")){
                if($i==0){$i++;continue;}//跳过第一行
                $coding = $item[5];
                $storeData = $this->sellpointModel->getNewData(array('Coding'=>$coding, 'SpState_sys'=>1,'isOpen'=>1));
                if (!$storeData){
                    $err ++;
                    $errorSellpoint[] = "【".gbk2utf8($item[4])."】的编码【".$coding."】错误";
                    continue;
                }
                $type = (int)$item[6];
                $where = array(
                    'storeId' => (int)$storeData['userId'],
                    'timeUnit' => 0,
                    'year' => (int)$year,
                );
                $data = array(
                    'area' => $storeData['area'],
                    'region' => $storeData['region'],
                    'spsarea' => $storeData['spsarea'],
                    'city' => $storeData['city'],
                    'coding' => $storeData['coding'],
                    'storeName' => $storeData['storeName'],
                    'type' => $type,
                    'createTime' => time()
                );
                if ($this->spreadStoreModel->upset($where, $data)){
                    $ok ++;
                }else{
                    $err ++;
                }
                usleep(500);
            }
            showSuccess('导入完成：（成功条数：'.$ok.',失败条数：'.$err.'）', '', 3);
        }else{
            //年
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            $this->load->view('Import/spreadStoreAdd', $this->viewData);
        }
    }
    /**
     * 固定传播基金专营店类删除
     * @return [type] [description]
     */
    function spreadStoreDel(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的数据');
        //载入单店主线流程模型
        $this->load->model('spreadStoreModel');
        if ($this->spreadStoreModel->del($id)){
            showError('删除成功', printUrl('Import','spreadStore'));
        }else{
            showError('删除失败');
        }

    }
    /**
     * 上传文件
     * @return array
     */
    function upload(){
        //允许上传文件格式
        $config['allowed_types'] = 'csv';
        $extPath = date('Ymd');//子目录
        //上传目录
        $config['upload_path'] = '../upload/'. $extPath .'/';
        //网站目录
        $webPath = substr($config['upload_path'], 2);
        //判断上传目录是否存在，不存在则创建
        if (!is_dir($config['upload_path'])){
            mkdir($config['upload_path'],0777);
        }
        //是否重命名
        $config['encrypt_name'] = TRUE;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('inputFile'))
        {
            $error = $this->upload->display_errors('','');
            showError($error);
        } else {
            $data = $this->upload->data();
            return $data;
        }
    }
}